
package com.zsrt.day20170424.homework.ch01;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.Reader;
import java.io.Writer;


import org.junit.Test;




public class AskTest {

	

	@Test
	public void askTest() throws Exception {
		
		File file = new File("shuchu/ask.txt");
		Reader is = new FileReader(file);
		BufferedReader bw = new BufferedReader(is);
		
		File file1= new File("shuchu/answer.txt");
		Writer w = new FileWriter(file1);
		BufferedWriter writer = new BufferedWriter(w);
		
		String string= null;
		while (( string=bw.readLine()) !=null) {
			
			
			if(string.contains("+")){
				
				//把等号去掉
				string= string.substring(0, string.length()-1);
				
				//分割            \\转义符号
				String[] strings = string.split("\\+");
				
				int a = Integer.parseInt(strings[0]);
				int b = Integer.parseInt(strings[1]);
				int sum =add(a, b);	
				//写入
				writer.write(string+"="+""+sum);
				//换行
				writer.newLine();
			}else if(string.contains("-")){
				string= string.substring(0, string.length()-1);
				String[] strings = string.split("\\-");
				
				int a = Integer.parseInt(strings[0]);
				int b = Integer.parseInt(strings[1]);
				
				int sub = sub(a,b);
				writer.write(string+"="+""+sub+"\n");
			}else if(string.contains("*")){
				string= string.substring(0, string.length()-1);
				String[] strings = string.split("\\*");
				
				int a = Integer.parseInt(strings[0]);
				int b = Integer.parseInt(strings[1]);
				
				int multi = mul(a,b);
				writer.write(string+"="+""+multi+"\n");
			}else if(string.contains("/")){
				string= string.substring(0, string.length()-1);
				String[] strings = string.split("\\/");
				
				int a = Integer.parseInt(strings[0]);
				int b = Integer.parseInt(strings[1]);
				int div =  div(a,b);
				writer.write(string+"="+""+div+"\n");
			}else if(string.contains("%")){
				string= string.substring(0, string.length()-1);
				String[] strings = string.split("\\%");
				
				int a = Integer.parseInt(strings[0]);
				int b = Integer.parseInt(strings[1]);
				int mo =  qiumo(a,b);
				writer.write(string+"="+""+mo+"\n");
			}else{
				System.out.println("错误");
			}
			
		}
		writer.flush();
		writer.close();
		
		bw.close();
	}
	
	/**
	 * 求模
	 * @param a
	 * @param b
	 * @return
	 */
	private int qiumo(int a, int b) {
		return a%b;
	}

	/**
	 * 除法
	 * @param a
	 * @param b
	 * @return
	 */
	private int div(int a, int b) {
		return a/b;
	}

	/**
	 * 乘法
	 * @param a
	 * @param b
	 * @return
	 */
	private int mul(int a, int b) {
		return a*b;
	}

	/**
	 * 减法
	 * @param a
	 * @param b
	 * @return
	 */
	private int sub(int a, int b) {
		return a-b;
	}

	/**
	 * 加法
	 * @param a
	 * @param b
	 * @return
	 */
	public int add(int a,int b){
		return a+b;
	}
}
